package com.ytjj.qmyx.mall.controller.app;

import com.alibaba.fastjson.JSONObject;
import com.ytjj.common.api.CommonResult;
import com.ytjj.common.service.RedisService;
import com.ytjj.common.utils.TokenUtil;
import com.ytjj.qmyx.mall.constants.RedisConstants;
import com.ytjj.qmyx.mall.enums.ActivityTypeEnums;
import com.ytjj.qmyx.mall.model.dto.OldForNewProductDto;
import com.ytjj.qmyx.mall.model.response.OldForNewColumnProductListResponse;
import com.ytjj.qmyx.mall.model.response.OldForNewColumnResponse;
import com.ytjj.qmyx.mall.model.response.OldForNewIndexResponse;
import com.ytjj.qmyx.mall.service.ActivityService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 以旧换新 品牌补贴
 *
 *  *
 **/
@RestController
@RequestMapping("/app/oldfornew")
public class AppOldForNewController {

    @Resource
    private ActivityService activityService;

    @Resource
    private RedisService redisService;

    /**
     * 推荐领取商品列表
     *
     */
    @GetMapping("list")
    public CommonResult getList (@RequestParam Integer pageNum,
                                 @RequestParam Integer pageSize,
                                 @RequestParam(required = false) Integer columnId) {
        List<OldForNewProductDto> list = activityService.getList(pageNum, pageSize, columnId);
        if(columnId == null && list != null && list.size() > 0) {
            Collections.shuffle(list);
        }
        return CommonResult.page(list);
    }

    @GetMapping("index")
    public CommonResult index () {
        OldForNewIndexResponse oldForNewIndexResponse=new OldForNewIndexResponse();
        List<OldForNewProductDto> list = activityService.getList(1,20, 14);
        List<OldForNewProductDto> list1 = activityService.getList(1,3, 15);
        List<OldForNewProductDto> list2 = activityService.getList(1,20, null);
        oldForNewIndexResponse.setRecordSubsidyDataDto(activityService.getUserSubsidyInfo(RedisConstants.RECORD_SUBSIDYKEY_APP));
        oldForNewIndexResponse.setRecommendProductList(list);
        oldForNewIndexResponse.setRankProductList(list1);
        oldForNewIndexResponse.setProductList(list2);
        return CommonResult.success(oldForNewIndexResponse);
    }
    /**
     * 补贴中心
     * @return
     */
    @GetMapping("/queryOldForNewCenter")
    public CommonResult queryOldForNewCenter() {
        OldForNewColumnProductListResponse response=new OldForNewColumnProductListResponse();
        List<OldForNewColumnResponse> columnResponseList=new ArrayList<>();
        BigDecimal activityMin=BigDecimal.ZERO;
        BigDecimal activityMax=new BigDecimal(4000);

        String subsidyKey = com.ytjj.qmyx.mall.constants.RedisConstants.SUBSIDYKEY_APP;
        String token = TokenUtil.getToken();
        String key = com.ytjj.qmyx.mall.constants.RedisConstants.ACTIVITY_PRODUCT + ActivityTypeEnums.OLD_FOR_NEW.name() + TokenUtil.getDataType() + "_app_center";
        String s = redisService.get(key);
        if (StringUtils.isNotEmpty(s)) {
            response= JSONObject.parseObject(s,OldForNewColumnProductListResponse.class);
            response.setColumnList(response.getColumnList().stream().map(item -> {
                Collections.shuffle(item.getProductList());
                return item;
            }).collect(Collectors.toList()));
            return CommonResult.success(response);
        }else {
            List<OldForNewProductDto> productList = null;
            //15---限时补贴
            productList = activityService.queryOldForNewList(15, 1, 9, activityMin, activityMax,0);
            productList = productList.stream()
                    .map(i -> {
                        Map<Object, Object> subsidyHash = redisService.getHashEntries(subsidyKey + token);
                        if (subsidyHash != null && subsidyHash.size() > 0) {
                            Object subsidyData = subsidyHash.get(i.getId().toString());
                            if (null != subsidyData && StringUtils.isNotEmpty((String) subsidyData)) {
                                i.setIsReceiveSubsidy(1);
                            } else {
                                i.setIsReceiveSubsidy(0);
                            }
                        }
                        return i;
                    })
                    .collect(Collectors.toList());
            OldForNewColumnResponse oldForNewColumnResponse = new OldForNewColumnResponse();
            oldForNewColumnResponse.setId(15);
            oldForNewColumnResponse.setTitle("限时补贴");
            oldForNewColumnResponse.setProductList(productList);
            columnResponseList.add(oldForNewColumnResponse);

            response.setColumnList(columnResponseList);

            redisService.set(key, JSONObject.toJSONString(response));
            redisService.expire(key, 60);
        }
        return CommonResult.success(response);
    }

    /**
     * 补贴中心列表
     *
     * @return
     */
    @GetMapping("/queryOldForNewCenterListByPage")
    public CommonResult queryOldForNewCenterListByPage(@RequestParam(value = "columnId", required = false, defaultValue = "0") Integer columnId,
                                                       @RequestParam(value = "pageSize", required = false, defaultValue = "20") Integer pageSize,
                                                       @RequestParam(value = "pageNum", required = false, defaultValue = "1") Integer pageNum,
                                                       @RequestParam(value = "flag", required = false, defaultValue = "0") Integer flag,
                                                       @RequestParam(value = "sort", required = false, defaultValue = "0") Integer sort) {

        BigDecimal activityMin=BigDecimal.ZERO;
        BigDecimal activityMax=new BigDecimal(4000);
        if(1==flag){
            activityMin=new BigDecimal(0);
            activityMax=new BigDecimal(500);
        }else if(2==flag){
            activityMin=new BigDecimal(501);
            activityMax=new BigDecimal(1000);
        }else if(3==flag){
            activityMin=new BigDecimal(1001);
            activityMax=new BigDecimal(2000);
        }
        List<OldForNewProductDto> productList = null;
        productList = activityService.queryOldForNewList(columnId, pageNum, pageSize,activityMin, activityMax,sort);

        String subsidyKey = com.ytjj.qmyx.mall.constants.RedisConstants.SUBSIDYKEY_APP;
        String token = TokenUtil.getToken();

        productList = productList.stream()
                .map(i -> {
                    Map<Object, Object> subsidyHash = redisService.getHashEntries(subsidyKey + token);
                    if (subsidyHash != null && subsidyHash.size() > 0) {
                        Object subsidyData = subsidyHash.get(i.getId().toString());
                        if (null != subsidyData && StringUtils.isNotEmpty((String) subsidyData)) {
                            i.setIsReceiveSubsidy(1);
                        }else {
                            i.setIsReceiveSubsidy(0);
                        }
                    }
                    return i;
                })
                .collect(Collectors.toList());
        return CommonResult.success(productList);
    }

    /**
     * 以旧换新列表
     *
     * @return
     */
    @GetMapping("/queryOldForNewListByPage")
    public CommonResult queryOldForNewListByPage(@RequestParam(value = "columnId", required = false, defaultValue = "0") Integer columnId,
                                                 @RequestParam(value = "pageSize", required = false, defaultValue = "20") Integer pageSize,
                                                 @RequestParam(value = "pageNum", required = false, defaultValue = "1") Integer pageNum,
                                                 @RequestParam(value = "flag", required = false, defaultValue = "0") Integer flag,
                                                 @RequestParam(value = "sort", required = false, defaultValue = "0") Integer sort) {

        BigDecimal activityMin=BigDecimal.ZERO;
        BigDecimal activityMax=new BigDecimal(4000);
        if(1==flag){
            activityMin=new BigDecimal(499);
            activityMax=new BigDecimal(500);
        }else if(2==flag){
            activityMin=new BigDecimal(999);
            activityMax=new BigDecimal(1000);
        }else if(3==flag){
            activityMin=new BigDecimal(1999);
            activityMax=new BigDecimal(2000);
        }
        List<OldForNewProductDto> productList = null;
        productList = activityService.queryOldForNewList(columnId, pageNum, pageSize,activityMin, activityMax,sort);

        String subsidyKey = com.ytjj.qmyx.mall.constants.RedisConstants.SUBSIDYKEY_APP;
        String token = TokenUtil.getToken();

        productList = productList.stream()
                .map(i -> {
                    Map<Object, Object> subsidyHash = redisService.getHashEntries(subsidyKey + token);
                    if (subsidyHash != null && subsidyHash.size() > 0) {
                        Object subsidyData = subsidyHash.get(i.getId().toString());
                        if (null != subsidyData && StringUtils.isNotEmpty((String) subsidyData)) {
                            i.setIsReceiveSubsidy(1);
                        }else {
                            i.setIsReceiveSubsidy(0);
                        }
                    }
                    return i;
                })
                .collect(Collectors.toList());
        return CommonResult.success(productList);
    }

    /**
     * 以旧换新栏目商品列表
     *
     * @return
     */
    @GetMapping("/queryOldForNewColumnList")
    public CommonResult queryOldForNewColumnList(@RequestParam(value = "flag", required = false, defaultValue = "0") Integer flag) {
        OldForNewColumnProductListResponse response=new OldForNewColumnProductListResponse();
        List<OldForNewColumnResponse> columnResponseList=new ArrayList<>();
        BigDecimal activityMin=BigDecimal.ZERO;
        BigDecimal activityMax=new BigDecimal(4000);
        if(1==flag){
            activityMin=new BigDecimal(499);
            activityMax=new BigDecimal(500);
        }else if(2==flag){
            activityMin=new BigDecimal(999);
            activityMax=new BigDecimal(1000);
        }else if(3==flag){
            activityMin=new BigDecimal(1999);
            activityMax=new BigDecimal(2000);
        }
        String subsidyKey = com.ytjj.qmyx.mall.constants.RedisConstants.SUBSIDYKEY;
        String token = TokenUtil.getToken();
        String platformData = TokenUtil.getPlatformData();
        String key = com.ytjj.qmyx.mall.constants.RedisConstants.ACTIVITY_PRODUCT + ActivityTypeEnums.OLD_FOR_NEW.name() + TokenUtil.getDataType() + "_APP_" + flag;
        String s = redisService.get(key);
        if (StringUtils.isNotEmpty(s)) {
            response=JSONObject.parseObject(s,OldForNewColumnProductListResponse.class);
            response.setColumnList(response.getColumnList().stream().map(item -> {
                Collections.shuffle(item.getProductList());
                return item;
            }).collect(Collectors.toList()));
            return CommonResult.success(response);
        }else {
            List<OldForNewProductDto> productList = null;
            //14---专享补贴
            productList = activityService.queryOldForNewList(14, 1, 100, activityMin, activityMax,0);
            productList = productList.stream()
                    .map(i -> {
                        Map<Object, Object> subsidyHash = redisService.getHashEntries(subsidyKey + token);
                        if (subsidyHash != null && subsidyHash.size() > 0) {
                            Object subsidyData = subsidyHash.get(i.getId().toString());
                            if (null != subsidyData && StringUtils.isNotEmpty((String) subsidyData)) {
                                i.setIsReceiveSubsidy(1);
                            } else {
                                i.setIsReceiveSubsidy(0);
                            }
                        }
                        return i;
                    })
                    .collect(Collectors.toList());
            OldForNewColumnResponse oldForNewColumnResponse = new OldForNewColumnResponse();
            oldForNewColumnResponse.setId(14);
            oldForNewColumnResponse.setTitle("专享补贴");
            oldForNewColumnResponse.setProductList(productList);
            columnResponseList.add(oldForNewColumnResponse);
            //37---大额补贴
            productList = activityService.queryOldForNewList(37, 1, 100, activityMin, activityMax,0);
            productList = productList.stream()
                    .map(i -> {
                        Map<Object, Object> subsidyHash = redisService.getHashEntries(subsidyKey + token);
                        if (subsidyHash != null && subsidyHash.size() > 0) {
                            Object subsidyData = subsidyHash.get(i.getId().toString());
                            if (null != subsidyData && StringUtils.isNotEmpty((String) subsidyData)) {
                                i.setIsReceiveSubsidy(1);
                            } else {
                                i.setIsReceiveSubsidy(0);
                            }
                        }
                        return i;
                    })
                    .collect(Collectors.toList());
            oldForNewColumnResponse = new OldForNewColumnResponse();
            oldForNewColumnResponse.setId(37);
            oldForNewColumnResponse.setTitle("大额补贴");
            oldForNewColumnResponse.setProductList(productList);
            columnResponseList.add(oldForNewColumnResponse);
            //47--品牌补贴
            productList = activityService.queryOldForNewList(47, 1, 100, activityMin, activityMax,0);
            productList = productList.stream()
                    .map(i -> {
                        Map<Object, Object> subsidyHash = redisService.getHashEntries(subsidyKey + token);
                        if (subsidyHash != null && subsidyHash.size() > 0) {
                            Object subsidyData = subsidyHash.get(i.getId().toString());
                            if (null != subsidyData && StringUtils.isNotEmpty((String) subsidyData)) {
                                i.setIsReceiveSubsidy(1);
                            } else {
                                i.setIsReceiveSubsidy(0);
                            }
                        }
                        return i;
                    })
                    .collect(Collectors.toList());
            oldForNewColumnResponse = new OldForNewColumnResponse();
            oldForNewColumnResponse.setId(47);
            oldForNewColumnResponse.setTitle("品牌补贴");
            oldForNewColumnResponse.setProductList(productList);
            columnResponseList.add(oldForNewColumnResponse);
            //48---换新补贴
            productList = activityService.queryOldForNewList(48, 1, 100, activityMin, activityMax,0);
            productList = productList.stream()
                    .map(i -> {
                        Map<Object, Object> subsidyHash = redisService.getHashEntries(subsidyKey + token);
                        if (subsidyHash != null && subsidyHash.size() > 0) {
                            Object subsidyData = subsidyHash.get(i.getId().toString());
                            if (null != subsidyData && StringUtils.isNotEmpty((String) subsidyData)) {
                                i.setIsReceiveSubsidy(1);
                            } else {
                                i.setIsReceiveSubsidy(0);
                            }
                        }
                        return i;
                    })
                    .collect(Collectors.toList());
            oldForNewColumnResponse = new OldForNewColumnResponse();
            oldForNewColumnResponse.setId(48);
            oldForNewColumnResponse.setTitle("换新补贴");
            oldForNewColumnResponse.setProductList(productList);
            columnResponseList.add(oldForNewColumnResponse);
            response.setColumnList(columnResponseList);

            redisService.set(key, JSONObject.toJSONString(response));
            redisService.expire(key, 60);
        }
        return CommonResult.success(response);
    }

    /**
     * 以旧换新栏目商品列表
     *
     * @return
     */
    @GetMapping("/queryOldForNewColumnList2")
    public CommonResult queryOldForNewColumnList2(@RequestParam(value = "flag", required = false, defaultValue = "0") Integer flag,
                                                  @RequestParam(value = "pageNum", required = false, defaultValue = "1")Integer pageNum,
                                                  @RequestParam(value = "pageSize", required = false, defaultValue = "20")Integer pageSize,Integer columnId) {
        OldForNewColumnProductListResponse response=new OldForNewColumnProductListResponse();
        List<OldForNewColumnResponse> columnResponseList=new ArrayList<>();
        BigDecimal activityMin=BigDecimal.ZERO;
        BigDecimal activityMax=new BigDecimal(4000);
        if(1==flag){
            activityMin=new BigDecimal(499);
            activityMax=new BigDecimal(500);
        }else if(2==flag){
            activityMin=new BigDecimal(999);
            activityMax=new BigDecimal(1000);
        }else if(3==flag){
            activityMin=new BigDecimal(1999);
            activityMax=new BigDecimal(2000);
        }
        String subsidyKey = com.ytjj.qmyx.mall.constants.RedisConstants.SUBSIDYKEY;
        String token = TokenUtil.getToken();
        List<OldForNewProductDto> productList = null;
        //14---专享补贴
        productList = activityService.queryOldForNewList(columnId, pageNum, pageSize, activityMin, activityMax,0);
        productList = productList.stream()
                .map(i -> {
                    Map<Object, Object> subsidyHash = redisService.getHashEntries(subsidyKey + token);
                    if (subsidyHash != null && subsidyHash.size() > 0) {
                        Object subsidyData = subsidyHash.get(i.getId().toString());
                        if (null != subsidyData && StringUtils.isNotEmpty((String) subsidyData)) {
                            i.setIsReceiveSubsidy(1);
                        } else {
                            i.setIsReceiveSubsidy(0);
                        }
                    }
                    return i;
                })
                .collect(Collectors.toList());
        OldForNewColumnResponse oldForNewColumnResponse = new OldForNewColumnResponse();
        oldForNewColumnResponse.setId(columnId);
//            oldForNewColumnResponse.setTitle("专享补贴");
        oldForNewColumnResponse.setProductList(productList);
        columnResponseList.add(oldForNewColumnResponse);
        response.setColumnList(columnResponseList);

        return CommonResult.success(response);
    }

}
